package com.fr.report.script.function;

import com.fr.report.script.Function;
import com.fr.report.script.Function.Type;
import com.fr.report.script.NormalFunction;
import com.fr.report.script.Primitive;
import com.fr.util.Utils;

public class BITOPERATION extends NormalFunction
{
  public Object run(Object[] paramArrayOfObject)
  {
    if ((paramArrayOfObject.length < 3) || (paramArrayOfObject.length > 3))
      return Primitive.ERROR_NAME;
    Object localObject = paramArrayOfObject[2];
    long l1 = Utils.objectToNumber(paramArrayOfObject[0], false).longValue();
    long l2 = Utils.objectToNumber(paramArrayOfObject[1], false).longValue();
    if ("&".equals(localObject))
      return convert(l1 & l2);
    if ("|".equals(localObject))
      return convert(l1 | l2);
    if ("^".equals(localObject))
      return convert(l1 ^ l2);
    if ("<<".equals(localObject))
      return convert(l1 << (int)l2);
    if (">>".equals(localObject))
      return convert(l1 >> (int)l2);
    if (">>>".equals(localObject))
      return convert(l1 >>> (int)l2);
    if ("^~".equals(localObject))
      return convert(l1 ^ l2 ^ 0xFFFFFFFF);
    return Primitive.ERROR_NAME;
  }

  private Object convert(long paramLong)
  {
    if ((paramLong > 2147483647L) || (paramLong < -2147483648L))
      return new Long(paramLong);
    return new Integer((int)paramLong);
  }

  public Function.Type getType()
  {
    return Function.LOGIC;
  }

  public String getCN()
  {
    return "BITOPERATIOIN(int,int,op) 位运算，返回两个整数根据op进行位运算后的结果。\nint:十进制整数。\nop:位运算操作符，支持\"&\"(与),\"|\"(或),\"^\"(异或),\"<<\"(左移),\">>\"(右移)。\n示例：\nBITOPERATION(4,2,\"&\")表示4与2进行\"与\"运算,结果等于0。\nBITOPERATION(4,2,\"|\")表示4与2进行\"或\"运算,结果等于6。\nBITOPERATION(4,2,\"^\")表示4与2进行\"异或\"运算,结果等于6。\nBITOPERATION(4,2,\"<<\")表示4按位左移2位，结果等于16。\nBITOPERATION(4,2,\">>\")表示4按位右移2位，结果等于1。\nBITOPERATION(4,1,\"^~\")表示4与2进行\"同或\"运算,结果为-7。";
  }

  public String getEN()
  {
    return "";
  }
}